<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript">
	$(function() {
		var resourceTrees = JSON.parse('${resouceTrees}');
		var roleTree = JSON.parse('${roleTrees}');
		$('#pid').combotree({
			data : roleTree,
			parentField : 'pid',
			lines : true,
			readonly:true,
			//panelHeight : 'auto',
			//value:'${parentId }',
			onLoadSuccess : function() {
				//parent.$.messager.progress('close');
				var node = $('#pid').combotree('tree').tree('getRoot');
				if(node){
					$('#pid').combotree("setValue",node.id);
				}
			}
		});
		
		$('#residenceId').combobox({
				onSelect : function(node){
					var treeUrl = '/roleController/residenceTree?residenceId=' + node.value;
					$('#pid').combotree('clear');
					$('#pid').combotree('reload',treeUrl);
				},
				onLoadSuccess:function(){
					var value = $('#residenceId').combobox('getValue');
					if(value && value != ''){
						var treeUrl = '/roleController/residenceTree?residenceId=' + value;
						$('#pid').combotree('reload',treeUrl);
					}
				}
		});
		$('#resourceIds').tree({
			//url : '/roleController/allTree',
			data : resourceTrees,
			parentField : 'pid',
			value:['${resourceIds }'],
			checkbox:true,
			lines : true,
			check:true,
			value:"",
			onLoadSuccess : function() {
				parent.$.messager.progress('close');				
			}
		});
		var getPtreeId = function(node,nodeMap){
			var pnode = $('#resourceIds').tree('getParent',node.target);
			if(pnode){
					nodeMap[pnode.id] = '1';
					getPtreeId(pnode,nodeMap);
			}
		};
		
		$('#form').form({
			url : '/roleController/add',
			onSubmit : function() {
				parent.$.messager.progress({
					title : '提示',
					text : '数据处理中，请稍后....'
				});
				var isValid = $(this).form('validate');
				var nodes = $('#resourceIds').tree('getChecked');
				if(nodes){
					var nodeMap = {};
					var nodestrs = '';
					for(var key in nodes){
						nodeMap[nodes[key].id] = '1';
					 	 getPtreeId(nodes[key],nodeMap);
					}
					for(var k in nodeMap){
						if(nodestrs != ''){
							nodestrs += ',';
						} 
						nodestrs += k;
					}
					
					$('#form input[name="resourceIds"]').val(nodestrs);
				}
				
				if (!isValid) {
					parent.$.messager.progress('close');
				}
				return isValid;
			},
			success : function(result) {
				parent.$.messager.progress('close');
				result = $.parseJSON(result);
				if (result.success) {
					parent.$.modalDialog.openner_treeGrid.treegrid('reload');//之所以能在这里调用到parent.$.modalDialog.openner_treeGrid这个对象，是因为role.jsp页面预定义好了
					parent.$.modalDialog.handler.dialog('close');
				}else {
					parent.$.messager.alert("提示",result.msg, "info");
				}
			}
		});
	});
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'center',border:false" title="" style="overflow: hidden;width: 100%;height: 100%">
		<form id="form" method="post" style="width: 100%;height: 100%">
			<table class="table table-hover table-condensed"  style="width: 100%;height: 100%">
				<tr>
				<th>选择小区</th>
				<c:choose>
					<c:when test="${isAdmin }">
						<td>
							<select name="residenceId" id="residenceId"  data-options="editable:false,required:true,width:390" >
									<c:forEach items="${residences }" var="r">
											<c:choose>
													<c:when test="${residenceId ==  r.id }">
														<option value="${r.id }" selected="selected">${r.name }</option>
													</c:when>
													<c:otherwise>
														<option value="${r.id }" >${r.name }</option>
													</c:otherwise>
											</c:choose>
											
									</c:forEach>
									
							</select>
						</td>
							
					</c:when>
					<c:otherwise>
						<td>
						${residence.name }<input name="residenceId" value="${residence.id }" type="hidden"/>
						
						</td>
					</c:otherwise>
				</c:choose>
				</tr>
				<tr>
					<th>上级角色</th>
					<td>
							<input  name="parentId" id="pid"  data-options="editable:false,required:true,width:390" />
				</td>
				</tr>
				<tr>
					<th>角色名称</th>
					<td><input name="name" type="text" placeholder="请输入角色名称" style="width:390px"  class="easyui-validatebox" data-options="width:390,required:true" value="" /></td>
				</tr>
				<tr>
					<th>排序</th>
					<td>	<input class="easyui-slider"  name="sortWeight" style="width:390px" data-options="min:1,max:100,showTip:true" />
				</tr>
				<tr>
					<th>备注</th>
					<td colspan=""><textarea name="remark" rows="" cols="" class="span5" style="width:390px" ></textarea></td>
				</tr>
				<tr style="height: 200px">
					<th>分配资源</th>
					<td style="overflow: auto;">
					<div id="resourceIds" style="height: 100%;width: 100%;overflow: auto;" ></div>
					<!-- 
						<input id="resourceIds" value="resourceIds" data-options="checkbox:true,editable:false,required:true,width:200,icons:[{
											iconCls:'icon-clear',
											handler: function(e){
												$(e.data.target).combotree('clear');
											}
										}]"  />
					 -->
					<input type="hidden" value="" name="resourceIds"/>
					</td>
				</tr>
			</table>
		</form>
	</div>
</div>